<?php 


	function get_blogs()
	{
		global $wpdb;
		$query = 'SELECT * FROM `'.$wpdb->prefix.'lazar_blogs` ORDER BY ID';
		$query_result = $wpdb->get_results($query);
		return $query_result;
	}
	
	function insert_in_blogs($id, $category, $page)
	{ 
		global $wpdb;
		$insert_query = "INSERT INTO `".$wpdb->prefix."lazar_blogs` (`id`, `category`, `page`) VALUES ('".$id."', '".$category."', '".$page."');";
		$insert = $wpdb->get_results($insert_query);	
	}
	
	if ( isset($_POST['add_new_blog']) && $_POST['add_new_blog'] == 'true')
	{
		add_new_blog();
	}
	
	function add_new_blog()
	{
		$insert_categories = '';
		if ( isset($_POST['blog_categories']) )
			$insert_categories = implode(',', $_POST['blog_categories']);

		$insert_page = '';
		if ( isset($_POST['blog_page']) )
			$insert_page = $_POST['blog_page'];
			
		if ( $insert_categories != '' )
			insert_in_blogs( NULL, $insert_categories, $insert_page );
		
		$refer = $_SERVER['HTTP_REFERER'];
		
		echo "<script language=\"JavaScript\">
        <!--
        document.location.href  = \"".$refer."\";
        -->
        </script>";
	}
	


	function create_wordpress_admin_blog_submenu()
	{
		add_submenu_page( 'my-top-level-handle', 'Blog Options', 'Blog Options', 'manage_options', 'my-submenu-blog-handle', 'blog_admin_page');
		add_action('admin_init', 'register_blog_settings');
	
	}
	
	function register_blog_settings()
	{
		register_setting('blog_options', 'category');
		register_setting('blog_options', 'page');
		register_setting('blog_options', 'database_action');
	}
	
	function blog_admin_page() {
		

	//CHECK FUNCTION
	function create_tables($table_name)
	{
		global $wpdb;
		
		if ($table_name == $wpdb->prefix.'lazar_blogs')
		{
			$create_query = 'CREATE TABLE `'.$wpdb->prefix.'lazar_blogs` (
							`id` INT NOT NULL AUTO_INCREMENT ,
							`category` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
							`page` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
							PRIMARY KEY (`id`)
						) ENGINE = MYISAM ;
							';
			$create = $wpdb->get_results($create_query);
		}
	}

	//CHECK FUNCTION
	function check_if_db_exists($database)
	{
		global $wpdb;
		if($wpdb->get_var("SHOW TABLES LIKE '".$database."'") != $database)
			return '';
		
		else 
			return 'table_exists';
	}

	global $wpdb;
	if ( check_if_db_exists($wpdb->prefix.'lazar_blogs') == '') create_tables($wpdb->prefix.'lazar_blogs');
	
	

	function delete_entry($id)
	{
		global $wpdb;
		$delete_query = 'DELETE FROM `'.$wpdb->prefix.'lazar_blogs` WHERE id='.$id.' LIMIT 1';
		$wpdb->get_results($delete_query);	
		
		$refer = $_SERVER['HTTP_REFERER'];
		
		echo "<script language=\"JavaScript\">
        <!--
        document.location.href  = \"".$refer."\";
        -->
        </script>";
	}
	
	if(isset($_GET['delete']))
		if($_GET['delete'] != '')
			delete_entry($_GET['delete']); // IF CALLED DELETES A SLIDE BY ID 
?>
	<script type="text/javascript" language="javascript">
		/* function for confirming you want to delete */
		function confirmAction()
		{
			return confirm("Are you sure you want to delete this blog?")
		}
		
		jQuery(document).ready(function() {
			jQuery('.header').click(function() {
				jQuery(this).next('.hidden_content').stop(true, true).toggle('fast');
			});
		});
		
		jQuery(document).ready(function() {
			jQuery("#blog_tabs").tabs();
			jQuery(".transform").jqTransform();
		});
	</script>
    
<div class="wrap">  
    <div id="icon-options-general" class="icon32"><br /></div>
    <h2>Blog Options</h2>  
    
     <?php
	$get_admin_warnings = admin_warnings();
	if ( $get_admin_warnings != '' )
	{
		echo '<div id="admin_warnings">';
			echo '<div id="admin_warning_icon"></div>';
			echo $get_admin_warnings;
		echo '</div>';
	}
	?>
    
<div id="blog_tabs" class="admin_tabs">
    <ul>
        <li>
            <a href="#tab_current_blogs">
                Current Blogs
            </a>
        </li>
        <li>
            <a href="#tab_add_blog">
                Add Blog
            </a>
        </li>
    </ul>

	
    
    <div id="tab_current_blogs">
    <?php
    $display_blogs = get_blogs();
		
		if (count($display_blogs) != 0)
		{
			echo' 
			<table id="sort_slider" class="display_elements" cellspacing="0">
				<thead>
					<tr>
						<th class="title">Name</th>
						<th class="title">Categories</th>
						<th>Actions</th>
					</tr>
				</thead>';
				
			echo '<tbody>';
				$i = 0; // to add class to TR 
				global $wpdb;
				foreach ( $display_blogs as $blog )
				{
			
					if ( $i == 0 ) echo '<tr class="alternate" valign="top">';
					else echo '<tr>';
						echo '<td>';
							$page_data = get_page( $blog->page );
							echo $page_data->post_title;
						echo '</td>';
						echo '<td>';
							$blog_categories = explode(',', $blog->category);
							foreach ( $blog_categories as $key => $blog_category )
							{
								if ( $key != 0 )
									echo ' + ';
									
								echo get_the_category_by_ID($blog_category);
							}
							
						echo '</td>';
						echo '<td valign="center" align="center">';
							echo '<a class="delete" onClick="return confirmAction()" href="?page=my-submenu-blog-handle&amp;delete='.$blog->id.'">Delete</a>';
						echo '</td>';
					echo '</tr>';
					
					$i++;
					if ( $i == 2 ) $i = 0;
				
				}  // END foreach ( $display_blog as $blog )
			echo '</tbody>';
		 
			echo '
				<tfoot>
					<tr>
						<th>Name</th>
						<th>Categories</th>
						<th>Actions</th>
					</tr>
				</tfoot>
			</table>
			';
		} // END if (count($display_blog) != 0)
		
		else 
		{
			echo '<div id="admin_warnings">';
				echo 'There are no Blogs';
			echo '</div>';
		}
	?>
    </div> <!-- #tab_current_blogs" -->
    
    
    <div id="tab_add_blog">
    	<form method="post" action="#tab_add_blog" class="transform">
        	<input type="hidden" name="add_new_blog" value="true" />
        
            <div class="settings_wrapper">
                <div class="header">
                    Add new blog
                </div>
                <div class="row_data">
        
                    <div class="row clearfix alternate">
                        <label>Categories</label>
                        <ul class="categories_listing">
                        <?php
                            global $wpdb;
                            
                            $categories = get_categories();
                            
                            foreach ($categories as $category)
                            {
                                echo '<li>';
                                    echo '<input id="post_'.$category->cat_ID.'" type="checkbox" name="blog_categories[]" value="'.$category->cat_ID.'" />';
                                    echo $category->name;
                                echo '</li>';										
                            }
                            
                        ?>
                        </ul>
                    </div>
                    
                    <div class="row clearfix">
                        <label>Pages</label>
                        <select name="blog_page">
                        <?php
                            global $wpdb;
                            $page_query = "SELECT * FROM $wpdb->posts WHERE post_type = 'page' ";
                            
                            $pages = $wpdb->get_results($page_query);
                            foreach ( $pages as $page )
                            {
                                echo '<option value="'.$page->ID.'">'.$page->post_title.'</option>';					
                            }
                        ?>
                        </select>
                    </div>
                    
                    <div class="row clearfix alternate">
                        <input name="Submit" type="submit" class="add_content_button" value="<?php esc_attr_e('Add blog'); ?>" />
                    </div>
                    
                </div>
                
                <div class="footer">
                    Add new blog
                </div>
            </div>
	
		</form>
   	</div> <!-- #tab_add_blog -->
    
    
</div> <!-- end #blog_tabs -->
    
</div> <!-- END  .wrap  -->  

<?php
	} // END blog_admin_page()
?>